package com.mayi.zds.service.imp;

import com.mayi.zds.dao.UserDAO;
import com.mayi.zds.entity.User;
import com.mayi.zds.service.UserService;
import com.mayi.zds.utils.GetComputerIP;
import com.mayi.zds.utils.ZdsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @ClassName UserServiceImp
 * @Author zhong
 * @Date 2019/12/24 12:01:18
 * @Version 1.0
 */
@Service
@Transactional
public class UserServiceImp implements UserService {
    @Resource
    private UserDAO userDAO;
    @Resource
    private HttpServletRequest request;

    /**
     * 增加用户
     *
     * @param user
     */
    @Override
    public int add(User user) {
        User user1 = userDAO.getUserByAccount(user.getAccount());
        if (user1 != null){
            return 0;
        }

        user.setId(ZdsUtils.produceId());
        user.setRole("1");
        user.setAvatar("user/1.jsp");
        user.setRegTime(new Date());
        user.setStatus("1");
        user.setUserGroup("1");
        userDAO.add(user);
        return 1;
    }

    /**
     * 删除用户
     *
     * @param userId
     */
    @Override
    public void del(String userId) {
        userDAO.del(userId);
    }

    /**
     * 修改用户
     *
     * @param user
     */
    @Override
    public void update(User user) {

        userDAO.update(user);
    }

    /**
     * 通过ID查询用户
     *
     * @param userId
     * @return
     */
    @Override
    public User getUserById(String userId) {
        User user = userDAO.getUserById(userId);
        return user;
    }

    /**
     * 查询所以用户
     *
     * @return
     */
    @Override
    public List<User> getUsers() {
        List<User> userList = getUsers();
        return userList;
    }

    /**
     * 根据账号和密码查询用户
     *
     * @param account
     * @param password
     * @return
     */
    @Override
    public User getUserByAccountAndPassword(String account, String password) {
        User user = userDAO.getUserByAccountAndPassword(account, password);
        return user;
    }

    @Override
    public User getUserByAccount(String account) {
        return userDAO.getUserByAccount(account);
    }

    @Override
    public String login(User user) {
        User user1 = userDAO.getUserByAccount(user.getAccount());
        if (user1 == null){
            return "该账号未注册";
        }
        if (!user1.getPassword().equals(user.getPassword())){
            return "密码错误";
        }
        return "登录成功";
    }

    @Override
    public int getOrderNum(String userid) {
        return userDAO.getOrderNum(userid);
    }

    @Override
    public int getSuccessOrderNum(String userid) {
        return userDAO.getSuccessOrderNum(userid);
    }

    @Override
    public int getCouponNum(String userid) {
        return userDAO.getCouponNum(userid);
    }
}
